/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 * 
 * Contact luchyx@riaevolution.com
 *
 * See www.riaevolution.com
 * See www.riaevolution.com/blog
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package  com.riaevolution.utils {
  	public class MathUtils {

		private static var _instance:MathUtils = null;
		private var __divider:int = 2;
		private var __dividers:Array;
		
		private function removeArray(arrayLook:Array,arrayFind:Array):Object {
			var xp:Object;
			var i:int = 0; 
            while (i < arrayLook.length) {
			    xp = arrayFind.indexOf(arrayLook[i]);
			    //trace(arrayLook[i]+" ? "+arrayLook+" @"+i+" - "+arrayFind+ " @"+xp);
			    //if we found something reset the an look again.
				if(xp != -1){
					arrayFind.splice(xp,1);
					arrayLook.splice(i,1);
					i = 0;
				}
				i++;
			}
			//trace("Simplified :"+arrayLook +" & "+ arrayFind);
			return displayRatio(arrayLook,arrayFind);
		}

		private function displayRatio(denom:Array,numer:Array):Object {
			var d:Number = 1;
			var n:Number = 1;
			for(var s:* in denom) {
				d = d*denom[s];
			}
			for(var t:* in numer) {
				n = n*numer[t];
			}
			var obj:Object = new Object();
			obj.denominator = d;
			obj.numerator = n;
			obj.constant = d/n;
			obj.ratio = d+":"+n;
			return obj;
		}
		private function primos(n:Number): void {
			
			var s:String = String(n/__divider);
			if(s.indexOf(".") != -1) {
				__divider++;
				if(__divider>=9) return;
				primos(n);
			}
			else {
				n = n/__divider;
				__dividers.push(__divider);
				primos(n);
			}
		}

		public static function calculateRatio(widthPixels:Number,heightPixels:Number):Object {
			if(MathUtils._instance == null) MathUtils._instance = new MathUtils();
			MathUtils._instance.__dividers = new Array();
			MathUtils._instance.__divider = 2;
			MathUtils._instance.primos(widthPixels);
			var div1:Array = MathUtils._instance.__dividers;
			//trace(widthPixels+" > MCM:"+div1.join(","));
			MathUtils._instance.__dividers = new Array();
			MathUtils._instance.__divider = 2;
			MathUtils._instance.primos(heightPixels);
			var div2:Array = MathUtils._instance.__dividers;
			//trace(heightPixels+" > MCM:"+div2.join(","));
			var obj:Object = MathUtils._instance.removeArray(div1,div2);
			//trace("Result:"+obj.ratio);
			return obj;
		}
	}
}